/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.worlds.ui.edge;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.util.List;
import net.worlds.LoadingView;
import net.worlds.Main;
import net.worlds.MenuView;
import net.worlds.Util;
import net.worlds.ui.edge.SiteChat.ChatMessage;


/**
 *
 * @author Jacob
 */
public class ChatPanel extends EStickyFrame{
    private final Color transparentWhite = new Color(255, 255, 255, 25);
    private ChatUpdater updater;
    private final Color infoColor = new Color(13, 162, 212);
    private int chatHeight;
    public ChatPanel(EComponent parent)
    {
        super(parent, -1, -1, 230, 15, 35, 15);
        setBackgroundColor(shadowColor);
        setBorderColor(Util.transparent);
    }
    @Override
    public void setVisible(boolean v)
    {
        super.setVisible(v);
        if(v)
        {
            updater = new ChatUpdater();
            new Thread(updater).start();
        }
        else if(updater != null)
        {
            updater.running = false;
            updater = null;
        }
    }
    @Override
    public void render(Graphics2D g)
    {
        g.setFont(EDGESettings.defaultFont);
        int w = getWidth();
        int h = getHeight();
        g.setColor(backgroundColor);
        g.fillRect(getX(), getY(), w, h);
        int y = getY() + this.getHeight() - 8;
        if(!Main.siteChatBarText.isEmpty())
        {
            g.setColor(transparentWhite);
            g.fillRect(getX(), y - (int) lineHeight, getWidth(), (int) (lineHeight + 8));
            g.setColor(Color.white);
            g.drawString("> "+Main.siteChatBarText, getX() + 4, y);
            y -= lineHeight + 4;
        }
            List<ChatMessage> chats = SiteChat.getChats();
            synchronized(chats)
            {
                for(int i = 0; (y > getY() + 8 && i < chats.size()); i++)
                {
                    ChatMessage msg = chats.get(i);
                    g.setColor(msg.color);
                    g.drawString(msg.message, getX() + 4, y);
                    y -= lineHeight + 4;
                }
            }
        renderChildren((g));
    }
    class ChatUpdater implements Runnable
    {
        public boolean running = true;
        int lastRequest;
        public void run()
        {
            try {
                Thread.sleep(500);
            } catch (InterruptedException ex) {
            }
            while(Main.getView() instanceof MenuView)
            {
                String r = Util.httpGet("http://"+Main.siteURL+"/frames/getChats.php?u="+Main.id+"&id="+lastRequest);
                if(!r.isEmpty())
                {
                    String[] parts = r.split("\n");
                    lastRequest = Integer.parseInt(parts[0]);
                    int n = Integer.parseInt(parts[1]);
                    for(int i = 2; i < n; i++)
                    {
                        String m = parts[i];
                        Color c = Color.white;
                        if(m.startsWith("&"))
                        {
                            m = m.substring(2);
                            c = infoColor;
                        }
                        SiteChat.addChat(c, m);
                    }
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                }
            }
        }
    }
}
